package com.bootdo.app.controller;

import com.bootdo.app.domain.AddressSession;
import com.bootdo.app.htmlVO.goods.GoodsDetail;
import com.bootdo.app.htmlVO.index.GoodsTJ;
import com.bootdo.app.htmlVO.index.Index;
import com.bootdo.app.htmlVO.index.Login;
import com.bootdo.app.htmlVO.index.SellerAndGoods;
import com.bootdo.app.htmlVO.seller.SellerAndImages;
import com.bootdo.app.service.AppIndexService;
import com.bootdo.app.service.AppSellerService;
import com.bootdo.app.utils.AppUtilXML;
import com.bootdo.app.utils.redis.AppRedisUtil;
import com.bootdo.app.utils.returnresponse.ServerResponse;
import com.bootdo.appsystem.domain.*;
import com.bootdo.appsystem.service.*;
import com.bootdo.common.controller.BaseController;
import com.bootdo.common.utils.HttpContextUtils;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.mail.Address;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 【@author 】 @fan
 * 【@Email  】 mooncluyed@outlook.com
 * 【类解释  】
 * 【@Date   】 2018/5/12 0012
 **/
@Controller
@RequestMapping("/")
public class AppIndexController extends BaseController {

    @Autowired
    private AppIndexService appIndexService;
    @Autowired
    private AppSellerService appSellerService;
    @Autowired
    private SellerService sellerService;
    @Autowired
    private ProducesinfoService producesinfoService;
    @Autowired
    private ImageinfoService imageinfoService;
    @Autowired
    private BannerService bannerService;
    @Autowired
    private SellercategoryService sellercategoryService;
    @Autowired
    private EvaluateService evaluateService;


/*页面部分，均为get请求模式*/
//    首页
    @GetMapping("appindex")
    public String toIndex(){
        return "app/index";
    }

//    密码登录页面
    @GetMapping("applogin")
    public String toAppLogin(){
        return "app/loginByPass";
    }

//    手机登录页面
    @GetMapping("apploginphone")
    public String toAppLoginByPhone(){
        return "app/loginByPhone";
    }

//    手机404页面
    @GetMapping("app404")
    public String to404(){
        return  "app/404";
    }

//    手机错误页面
    @GetMapping("apperror")
    public String toError(){
        return "app/err";
    }

//    商家列表
    @GetMapping("appsellerlist")
    public String toSellerList(){
        return "app/sellerList";
    }

//    商品列表
    @GetMapping("appgoodslist")
    public String toGoodsList (){
        return "app/goodsList";
    }

//    商品推荐列表
    @GetMapping("appgoodsTJlist")
    public String toGoodsTJList (){
        return "app/goodsListTJ";
    }


    /**
     * 存入session中数据
     * @param addressSession
     * @return
     */
    @ResponseBody
    @GetMapping("appAddSession")
    private ServerResponse appAddSession(AddressSession addressSession){
//        AddressSession address = (AddressSession)this.getRequest().getSession().getAttribute(AppUtilXML.SESSION_ADDRESS);
        System.out.println(addressSession);
        try {
            HttpContextUtils.getHttpServletRequest().getSession().setAttribute(AppUtilXML.SESSION_ADDRESS,addressSession);
            return ServerResponse.createBySuccess();
        }catch (RuntimeException e) {
            return ServerResponse.createByErrorMessage("位置获取失败");
        }
    }

    /**
     * 修改session中数据
     * @param addressSession
     * @return
     */
    @ResponseBody
    @GetMapping("appUpdateSession")
    private ServerResponse appUpdateSession(AddressSession addressSession){
//        AddressSession address = (AddressSession)this.getRequest().getSession().getAttribute(AppUtilXML.SESSION_ADDRESS);
        System.out.println(addressSession);
        try {
            HttpContextUtils.getHttpServletRequest().getSession().setAttribute(AppUtilXML.SESSION_ADDRESS,addressSession);
            return ServerResponse.createBySuccess();
        }catch (RuntimeException e) {
            return ServerResponse.createByErrorMessage("位置获取失败");
        }
    }

    /**
     * 获取session中数据
     * @return
     */
    @ResponseBody
    @GetMapping("appGetSession")
    private ServerResponse<AddressSession> appGetSession( ){
        AddressSession address = (AddressSession)HttpContextUtils.getHttpServletRequest().getSession().getAttribute(AppUtilXML.SESSION_ADDRESS);
        if(address == null){
            return ServerResponse.createByError();
        }
        return ServerResponse.createBySuccess(address);
    }



    /**
     * 商品主列表
     * @param params
     * @return
     */
    @ResponseBody
    @GetMapping("appmsgGoodsList")
    public ServerResponse<PageUtils> msgGoodsList(@RequestParam Map<String, Object> params){
        if (params.get("categoryid").equals("0")) {
            params.remove("categoryid");
        }
        params.put("isnew" , 1);
        params.put("isfree" , 1);
        Query query = new Query(params);
        List<ProducesinfoDO> producesinfoDOList = producesinfoService.list(query);
        int total = producesinfoService.count(query);
        PageUtils pageUtils = new PageUtils(producesinfoDOList, total);
        return ServerResponse.createBySuccess(pageUtils);
    }

    /**
     * 首页信息获取
     * @return
     */
    @ResponseBody
    @GetMapping("appmsgIndex")
    public ServerResponse<Index> msgIndex () {
        Index index = new Index();

        List<BannerDO> bannerList = bannerService.getIndexList(1);
        index.setBannerList(bannerList);

        List<SellercategoryDO> sellercategoryList = sellercategoryService.getIndexList();
        index.setSellercategoryList(sellercategoryList);

        List<SellerDO> indexSellerList = sellerService.getIndexList();
        index.setSellerList(indexSellerList);

        List<ProducesinfoDO> indexProducesinfoList = producesinfoService.getIndexList();
        index.setProducesinfoList(indexProducesinfoList);

        return ServerResponse.createBySuccess(index);
    }

    //商品推荐页数据
    @ResponseBody
    @GetMapping("appmsgGoodsTJ")
    public ServerResponse<GoodsTJ> msgGoodsTJ () {
        GoodsTJ goodsTJ = new GoodsTJ();

        List<BannerDO> bannerList = bannerService.getIndexList(2);
        goodsTJ.setBannerList(bannerList);

        List<SellerAndGoods> tjGoodsAll = producesinfoService.getTJGoodsAll();
        goodsTJ.setSellerAndGoodsList(tjGoodsAll);

        return ServerResponse.createBySuccess(goodsTJ);
    }

//    商家列表
    @ResponseBody
    @GetMapping("appmsgsellerlist")
    public ServerResponse<PageUtils> msgSellerList(@RequestParam Map<String, Object> params){
        params.put("status",1);
        Query query = new Query(params);
        List<SellerDO> sellerDOList = sellerService.list(query);
        int total = sellerService.count(query);
        PageUtils pageUtils = new PageUtils(sellerDOList, total);
        return ServerResponse.createBySuccess(pageUtils);
    }

//    商家详情页面
    @GetMapping("appsellerinfo/{sellerid}")
    public String toSellerInfoAndImage(@PathVariable("sellerid")Integer sellerid , Model model){
        SellerAndImages sellerAndImages = appSellerService.getSellerAndImages(sellerid);
        if(sellerAndImages.getSellerDO() == null){
            return "app/404";
        }
        model.addAttribute("sellerandimage" , sellerAndImages);
        return "app/sellerDetail";
    }

//    商品详情页
    @GetMapping("appGoodDetails/{gid}")
    public String toGoodDetails(@PathVariable("gid") Integer gid,Model model){
        //商品信息
        ProducesinfoDO produces = producesinfoService.get(gid);

        HashMap<String, Object> map = new HashMap<>();
        map.put("sort","display");
        map.put("sort","imageid");
        map.put("order","asc");
        map.put("allid",gid);
        map.put("type",12009);

        //商品轮播图
        List<ImageinfoDO> swlist = imageinfoService.list(map);
        map.put("type",12010);

        //商品主图
        List<ImageinfoDO> mainlist = imageinfoService.list(map);
        GoodsDetail goodsDetail = new GoodsDetail();
        goodsDetail.setProducesinfoDO(produces);
        goodsDetail.setSwlist(swlist);
        goodsDetail.setMainlist(mainlist);

        model.addAttribute("goodsDetail", goodsDetail);

        ArrayList arrayList = new ArrayList();

        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("pid" , gid);
        hashMap.put("isshow" , 1);
        hashMap.put("offset" , 0);
        hashMap.put("limit" , 5);
        List<EvaluateDO> list = evaluateService.list(hashMap);
        for (EvaluateDO evaluateDO : list) {
            HashMap<String, Object> eMap = new HashMap<>();
            eMap.put("allid" , evaluateDO.getEid());
            eMap.put("type" , 22556);
            eMap.put("imagetype" , 1);
            List<ImageinfoDO> imageinfoList = imageinfoService.list(eMap);

            HashMap<String, Object> evaluateAndImage = new HashMap<>();
            evaluateAndImage.put("evaluate" , evaluateDO);
            evaluateAndImage.put("imageinfoList" , imageinfoList);
            //yyyy.MM.dd G 'at' HH:mm:ss
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            evaluateAndImage.put("addtime" , simpleDateFormat.format(evaluateDO.getAddtime()));
            arrayList.add(evaluateAndImage);
        }
        model.addAttribute("evaluateAndImage",arrayList);

        return "app/goods/goodsDetail";
    }



//    登录请求验证
    @ResponseBody
    @PostMapping("appmsglogin")
    public ServerResponse appmsglogin(Login login){
        return appIndexService.msgLoginInfo(login);
    }






}
